package com.duang.framework.service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ServiceFactory {

	public Class<?> createServiceProxy(Class<?> superClass, Class<?> daoClazz)
			throws Exception {
		if (superClass.isInterface()) {

			return ServiceUtils.buildServiceProxyWithJavassist(superClass,
					superClass.getDeclaredMethods(), daoClazz);
		} else {
			List<Method> methodNeedToImplement;
			List<Method[]> methodImplList = new ArrayList<Method[]>();
			Method[] methodDefines = ServiceUtils.findServiceMethods(
					superClass, methodImplList);
			if (methodDefines == null) {
				throw new IllegalStateException(
						"must implement service interface or service interface need to be added Service annnotation");
			}
			methodNeedToImplement = Arrays.asList(methodDefines);
			ServiceUtils.filterMethodToBeImpled(methodImplList,
					methodNeedToImplement);
			return ServiceUtils.buildServiceProxyWithJavassist(superClass,
					methodNeedToImplement, null);
		}
	}
}
